package com.wyw.function.learning.demo9;

import com.wyw.function.learning.common.Discount;
import com.wyw.function.learning.common.Quote;
import com.wyw.function.learning.common.Shop;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @Title   对多个异步任务进行流水线操作
 * @Description     使用Discount服务，因为Discount是一种远程服务，所以我们在上述的代码中加了1s延迟
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class DemoClient24 {

    static final List<Shop> shops =
            Arrays.asList(
                    new Shop("wyw"),
                    new Shop("hello"),
                    new Shop("one"),
                    new Shop("two")
            );

    public static void main(String[] args) {
        log(() -> findPrices("wyw"));
    }


    public static List<String> findPrices(String product) {
        return shops.stream().map(shop -> shop.getPrice(product)).map(Quote::parse).map(Discount::applyDiscount).collect(Collectors.toList());
    }

    private static<T> T log(Supplier<T> supplier) {
        long start = System.currentTimeMillis();
        T t = supplier.get();
        System.out.println(System.currentTimeMillis() - start + ":ms");
        return t;
    }
}

